package labox.innovation.gameserver.model.actor.instance;

import labox.innovation.gameserver.model.itemcontainer.Warehouse;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.ActionFailed;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;
import labox.innovation.gameserver.network.serverpackets.WareHouseAndInventoryPacket;
import labox.innovation.gameserver.templates.chars.NpcTemplate;

public class FuzeWarehouseInstance extends FuzeNpcInstance{

	public FuzeWarehouseInstance(long objectId, NpcTemplate template) {
		super(objectId, template);
	}
	
//	@Override
//	public String getHtmlPath(int npcId, int val) {
//		String pom = "";
//
//		if (val == 0)
//			pom = "" + npcId;
//		else
//			pom = npcId + "-" + val;
//		return "data/chat/" + pom + ".xml";
////		return "data/script/warehouse/" + pom + ".htm";
////		return "data/WareHouse/WareHouse.xml";  //-- 测试用
//	}
	
	/**
	 * 取得背包和仓库物品清单
	 * @param player
	 */
	private void showWareHouseAndInventoryWindow(FuzePcInstance player) {
		player.sendPacket(ActionFailed.STATIC_PACKET);
		if (player.getWarehouse() == null) 
			return;
		
		player.sendPacket(new WareHouseAndInventoryPacket(player));
	}
	
	/**
	 * 整理仓库
	 * @return
	 */
	private void sortWareHouse(FuzePcInstance player) {
		if (player.getWarehouse().getWareHouseSize() > player.getWarehouse().getSize()) {
			
		} else {  //-- 没有足够的槽位整理仓库
			player.sendPacket(new SystemMessage(SystemMessageId.NOT_ENOUGH_SLOT));
		}
	}
	
	/**
	 * 仓库扩容
	 * @param newSize
	 * @param newSize
	 * @return
	 */
	private void extendWareHouseSize(FuzePcInstance player, int newSize, int silver) {	
		int newWareHouseSize = player.getWarehouse().getWareHouseSize() + newSize;
		
		if (Warehouse.MAX_SLOTS >= newWareHouseSize) {
			if (player.getMoneyNote() >= silver) {
				if (player.reduceMoneyNote("NPC仓库扩容", silver, null, true)) {
					player.getWarehouse().setWareHouseSize(newWareHouseSize);
					player.sendMessage("扩充后的仓库容积为：" + newWareHouseSize);
				} 
			} else {
				player.sendMessage("您目前的银票不够！");
			}
		} else { //-- 超过仓库可扩充容积上限
			player.sendPacket(new SystemMessage(SystemMessageId.EXCEED_WAREHOUSE_LIMIT));
		}

	}
	
	@Override
	public void onBypassFeedback(FuzePcInstance player, String command) {
		if (command.startsWith("WithdrawP") || command.startsWith("DepositP")) {  //-- 从仓库中取出物品
			showWareHouseAndInventoryWindow(player);
		} else if (command.startsWith("SortWareHouse")) {  //-- 整理仓库
			sortWareHouse(player);
		} else if (command.startsWith("ExtendWareHouseSize")) {  //-- 仓库扩容
			String[] str = command.split(" ");
			int iCount = Integer.parseInt(str[1]);  //-- 扩充仓库格子数
			int iSilver = Integer.parseInt(str[2]);  //-- 扩充仓库需要银票数
			extendWareHouseSize(player, iCount, iSilver);
		} else if (command.startsWith("ExtendWareHouse")) {
			showChatWindow(player, 1);
		} else {
			super.onBypassFeedback(player, command);
		}
	}
}
